package com.google.gwt.sample.dynatablerf.server.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ConvertUtil {

	public static String getParteInteira(double d) {
		return Integer.toString((int) d);
	}

	public static int convertToInt(String string) {
		string = string.replace(".", "");
		if (string.contains(",")) {
			int index = string.indexOf(",");
			string = string.substring(0, index);
		}
		return Integer.parseInt(string);
	}

	public static double convertToDouble(String string) {
		string = string.replace(".", "");
		string = string.replace(',', '.');
		return Double.parseDouble(string);
	}

	public static Date convertToDate(String string, String formatDate)
			throws ParseException {
		if (StringUtil.isEmpty(string)) {
			return null;
		} else {
			SimpleDateFormat formatter = new SimpleDateFormat(formatDate);
			return formatter.parse(string);
		}
	}
}
